package work.xiaohong.mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.xiaohong.mybatisplus.entity.Dept;
import work.xiaohong.mybatisplus.service.IDeptService;
import work.xiaohong.utils.Result;
import work.xiaohong.utils.StatusCode;

import java.util.List;

/**
 * 部门表 前端控制器
 * @author xiaohong
 * @since 2021-10-22 10:10:22
 */
@Api(tags = "Dept接口文档")
@RestController
@RequestMapping("/dept")
public class DeptController {

   @Autowired
   private IDeptService deptService;


    /***
    * 条件分页查询
    * @param dept
    * @param page
    * @param size
    * @return
    */
    @Operation(summary = "条件分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
        @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
        @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "Dept对象",required = true) Dept dept, @PathVariable(value="page")  int page, @PathVariable(value="size")  int size){
        Page<Dept> pageInfo = deptService.page(new Page<Dept>(page,size),new QueryWrapper<Dept>(dept));
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }


    /***
    * 分页查询
    * @param page:当前页
    * @param size:每页显示多少条
    * @return
    */
    @Operation(summary = "分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
        @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
        @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable(value="page")  int page, @PathVariable(value="size")  int size){
        //调用DeptService实现分页查询Dept
        Page<Dept> pageInfo = deptService.page(new Page<>(page,size));
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
    * 条件查询
    * @param dept
    * @return
    */
    @Operation(summary = "条件查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping(value = "/search" )
    public Result<List<Dept>> findList(@RequestBody(required = false) @Parameter(name = "Dept对象",required = true) Dept dept){
        List<Dept> list = deptService.list(new QueryWrapper<Dept>(dept));
        return new Result<List<Dept>>(true,StatusCode.OK.getCode(),"查询成功",list);
    }


    /***
    * 根据Id删除dept数据
    * @param id
    * @return
    */
    @Operation(summary = "根据ID删除Dept",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "删除对象ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable(value="id") Integer id){
        deptService.removeById(id);
        return new Result(true,StatusCode.OK.getCode(),"删除成功");
    }



    /***
    * 根据Id批量删除dept数据
    * @param ids
    * @return
    */
    @Operation(summary = "根据Id批量删除Dept",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @DeleteMapping
    public Result deleteByIds(@RequestBody @Parameter(name = "需要删除的Dept对象id集合",required = true) List<Integer> ids){
        deptService.removeByIds(ids);
        return new Result(true,StatusCode.OK.getCode(),"删除成功");
    }


    /***
    * 根据ID修改Dept数据
    * @param dept
    * @return
    */
    @Operation(summary = "根据ID修改Dept",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PutMapping
    public Result updateById(@RequestBody @Parameter(name = "Dept对象",required = true) Dept dept){
        deptService.updateById(dept);
        return new Result(true,StatusCode.OK.getCode(),"修改成功");
    }


     /***
     * 新增Dept数据
     * @param dept
     * @return
     */
     @Operation(summary = "添加Dept",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
     @PostMapping
     public Result add(@RequestBody  @Parameter(name = "Dept对象",required = true) Dept dept){
        deptService.save(dept);
        return new Result(true,StatusCode.OK.getCode(),"添加成功");
     }

     /***
     * 根据ID查询Dept数据
     * @param id
     * @return
     */
      @Operation(summary = "根据ID查询Dept",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
      @Parameter(in = ParameterIn.PATH, name = "id", description = "查询对象ID", required = true)
     @GetMapping("/{id}")
     public Result<Dept> findById(@PathVariable(value="id") Integer id){
        Dept dept = deptService.getById(id);
        return new Result<Dept>(true,StatusCode.OK.getCode(),"查询成功",dept);
     }

     /***
     * 根据Id批量查询Dept数据
     * @param ids
     * @return
     */
     @Operation(summary = "根据Id批量查询Dept数据",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
     @PostMapping("/ids")
     public Result<List<Dept>> findByIds(@RequestBody @Parameter(name = "需要查询的Dept对象id集合",required = true) List<Integer> ids){
        List<Dept> deptList = deptService.listByIds(ids);
        return new Result<List<Dept>>(true,StatusCode.OK.getCode(),"查询成功",deptList);
     }

     /***
     * 查询Dept全部数据
     * @return
     */
     @Operation(summary = "查询所有Dept",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
     @GetMapping
     public Result<List<Dept>> findAll(){
        List<Dept> list = deptService.list();
        return new Result<List<Dept>>(true, StatusCode.OK.getCode(),"查询成功",list) ;
     }


}
